package indi.storyoftime.rasite.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.storyoftime.rasite.entity.Album;
import indi.storyoftime.rasite.entity.Image;
import indi.storyoftime.rasite.mapper.AlbumMapper;
import indi.storyoftime.rasite.mapper.ImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class AlbumService {

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private AlbumMapper albumMapper;

    /**
     * 根据id查询相册记录。
     */
    public Album getAlbumById(Integer id) {
        QueryWrapper<Album> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("is_deleted", 0);
        return albumMapper.selectOne(wrapper);
    }

    /**
     * 查询所有相册记录。
     */
    public List<Album> getAlbums() {
        return albumMapper.selectList(new QueryWrapper<Album>().eq("is_deleted", 0));
    }

    /**
     * 分页查询相册记录。
     */
    public List<Album> getAlbumsByPage(Integer pageNo, Integer pageSize, String orderBy, boolean desc) {
        if (Objects.nonNull(pageNo) && Objects.nonNull(pageSize) && pageNo > 0 && pageSize > 0) {
            Page<Album> page = new Page<>(pageNo, pageSize);
            QueryWrapper<Album> wrapper = new QueryWrapper<Album>().eq("is_deleted", 0);
            if (Objects.nonNull(orderBy)) {
                wrapper.orderBy(true, !Objects.requireNonNullElse(desc, true), orderBy);
            }
            return albumMapper.selectPage(page, wrapper).getRecords();
        } else {
            return new ArrayList<Album>();
        }
    }

    public List<Album> getAlbumsByPage(Integer pageNo, Integer pageSize) {
        return getAlbumsByPage(pageNo, pageSize, "id", true);
    }

    /**
     * 使用limit查询相册记录
     */
    public List<Album> getAlbumsByLimit(Integer offset, Integer limit, String orderBy, boolean desc) {
        if (Objects.nonNull(offset) && Objects.nonNull(limit) && offset > 0 && limit > 0) {
            QueryWrapper<Album> wrapper = new QueryWrapper<>();
            wrapper.eq("is_deleted", 0);
            if (Objects.nonNull(orderBy)) {
                wrapper.orderBy(true, !Objects.requireNonNullElse(desc, true), orderBy);
            }
            wrapper.last("limit " + offset + "," + limit);
            return albumMapper.selectList(wrapper);
        } else {
            return new ArrayList<Album>();
        }
    }

    public List<Album> getAlbumsByLimit(Integer offset, Integer limit) {
        return getAlbumsByLimit(offset, limit, "id", true);
    }

    /**
     * 获取相册中的所有图片。
     */
    public List<Image> getImagesInAlbum(Integer albumId) {
        QueryWrapper<Image> wrapper = new QueryWrapper<>();
        wrapper.eq("album_id", albumId);
        wrapper.eq("is_deleted", 0);
        return imageMapper.selectList(wrapper);
    }

    /**
     * 分页获取相册中的图片。
     */
    public List<Image> getImagesInAlbumByPage(Integer albumId, Integer pageNo, Integer pageSize, String orderBy, Boolean desc) {
        if (Objects.nonNull(albumId) && Objects.nonNull(pageNo) && pageNo > 0) {
            if (!Objects.nonNull(pageSize) || pageSize < 0) {
                pageSize = 10;
            }
            Page<Image> page = new Page<>(pageNo, pageSize);
            QueryWrapper<Image> wrapper = new QueryWrapper<>();
            wrapper.eq("album_id", albumId);
            wrapper.eq("is_deleted", 0);
            if (Objects.nonNull(orderBy)) {
                wrapper.orderBy(true, !Objects.requireNonNullElse(desc, true), orderBy);
            }
            return imageMapper.selectPage(page, wrapper).getRecords();
        } else {
            return new ArrayList<>();
        }
    }

    public List<Image> getImagesInAlbumByPage(Integer albumId, Integer pageNo, Integer pageSize) {
        return getImagesInAlbumByPage(albumId, pageNo, pageSize, "id", true);
    }

    /**
     * 使用limit查询相册中的图片
     */
    public List<Image> getImagesInAlbumByLimit(Integer albumId, Integer offset, Integer limit, String orderBy, boolean desc) {
        if (Objects.nonNull(albumId) && Objects.nonNull(limit) && limit > 0) {
            if (!Objects.nonNull(offset) || offset < 0) {
                offset = 0;
            }
            QueryWrapper<Image> wrapper = new QueryWrapper<>();
            wrapper.eq("album_id", albumId);
            wrapper.eq("is_deleted", 0);
            if (Objects.nonNull(orderBy)) {
                wrapper.orderBy(true, !Objects.requireNonNullElse(desc, true), orderBy);
            }
            wrapper.last("limit " + offset + "," + limit);
            return imageMapper.selectList(wrapper);
        } else {
            return new ArrayList<>();
        }

    }

    public List<Image> getImagesInAlbumByLimit(Integer albumId, Integer offset, Integer limit) {
        return getImagesInAlbumByLimit(albumId, offset, limit, "id", true);
    }

    /**
     * 创建一个相册。
     */
    public Integer createAlbum(Album album) {
        album.setIsDeleted(0);
        return albumMapper.insert(album);
    }

    /**
     * 根据id更新相册。TODO：限制某些字段不可更新，无id时抛出错误。
     */
    public Integer updateAlbumById(Integer id, Album album) {
        album.setId(id);
        return albumMapper.updateById(album);
    }

    /**
     * 判断album是否存在
     */
    public Boolean isAlbumAvailable(Integer albumId) {
        if (!Objects.nonNull(albumId)) {
            return false;
        }
        Album album = albumMapper.selectById(albumId);
        return Objects.nonNull(album);
    }

    /**
     * 根据id删除相册。
     * 会关联删除其他图片与该相册的关联。
     */
    public void deleteAlbumById(Integer id) {
        Album album = new Album();
        album.setIsDeleted(1);
        QueryWrapper<Album> albumWrapper = new QueryWrapper<>();
        albumWrapper.eq("id", id);
        albumMapper.update(album, albumWrapper);
        imageMapper.updateAlbumIdToNull(id);
    }

}
